package com.qf.user.settings;

import com.qf.user.dao.SystemSettingDao;
import com.qf.user.model.SystemSetting;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.TreeMap;

/**
 * 配置类
 *
 * @author dsh
 */

@Component
public class Settings {

    public static final String TRY_MAX_COUNT = "try_max_count";
    public static final String START_TIME = "start_time";
    public static final String END_TIME = "end_time";


    @Resource
    private SystemSettingDao systemSettingDao;

    /**
     * map存储所有的 key  value
     */
    private TreeMap<String, String> treeMapSettings;

    /**
     * java 自带的注解，用来修饰一个非静态的方法，，并且智慧被服务器执行一次，在init方法之前执行
     */
    @PostConstruct
    public void initSettings() {

        //去数据库中查询数据库
        List<SystemSetting> systemSettings = systemSettingDao.selectByExample(null);
        //存到 map 中
        if (treeMapSettings != null && treeMapSettings.size() > 0) {
            for (int i = 0; i < systemSettings.size(); i++) {
                SystemSetting systemSetting = systemSettings.get(i);
                treeMapSettings.put(systemSetting.getKey(), systemSetting.getValue());
            }
        }
    }

    /**
     * 获取系统配置
     *
     * @param key
     * @return
     */
    public String get(String key) {

        return treeMapSettings.get(key);
    }
}
